package com.hdoit.ares.web.cust.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ICustomerService;
import com.hdoit.ares.logical.viewmodel.cust.TeamShareCustomer;
import com.tennetcn.component.webapi.BaseApi;
import com.tennetcn.component.webapi.ResponseModel;
import com.tennetcn.component.webapi.ResponseStatus;
import com.wordnik.swagger.annotations.ApiParam;

@Controller
@RequestMapping(value = "/api/v1/CustMyTeamAresApi/",method = { RequestMethod.GET,RequestMethod.POST},produces="application/json;charset=utf-8")
@Api(value = "/api/v1/CustMyTeamAresApi.java/")
@CrossOrigin(origins = "*",maxAge = 3600)
public class CustMyTeamAresApi extends BaseApi{
	
	private ICustomerService customerService;
	
	public ICustomerService getCustomerService() {
		return customerService;
	}
	@Resource
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}
	
	
	
	private final String teamContentApi = "teamContent";
	private final String teamShareApi = "teamShare";
	private final String currentTeamShareApi="currentTeamShare";
	private final String searchTeamApi= "searchTeam";
	
	@ResponseBody
	@RequestMapping(value= teamContentApi)
	@ApiOperation(notes= teamContentApi,httpMethod="POST",value="一代数 二代数")
	public String teamContent() {
	
		ResponseModel rm = new ResponseModel();
		Customer customer=customerService.queryModel(getLoginId()); 
		
		int oneTeamShareCount=customerService.queryOneTeamShareCount(customer.getId());
		int twoTeamShareCount=customerService.queryTwoTeamShareCount(customer.getId());
		
		rm.put("oneTeamShareCount", oneTeamShareCount);
		rm.put("twoTeamShareCount", twoTeamShareCount);
		
		return rm.toJson();
	}
	
	@ResponseBody
	@RequestMapping(value= teamShareApi)
	@ApiOperation(notes= teamShareApi,httpMethod="POST",value="团队列表")
	public String teamShare(
			@ApiParam("第几代") @RequestParam(value="category",required=true)String category
			) {
		Customer customer=customerService.queryModel(getLoginId()); 
		ResponseModel rm = new ResponseModel();
		List<TeamShareCustomer> teamShareCustomers=null;
		
		if("one".equals(category)){
			teamShareCustomers=customerService.queryOneTeamShare(customer.getId());
		}else if("two".equals(category)){
			teamShareCustomers=customerService.queryTwoTeamShare(customer.getId());
		}
		if(teamShareCustomers.size()>=0){
			rm.setStatus(ResponseStatus.success);
			List<String> params = new ArrayList<String>();
			Field[] fields = TeamShareCustomer.class.getDeclaredFields();
			for(Field field : fields){
				params.add(field.getName());
			}
			rm.putList("teamShareCustomers", teamShareCustomers, params);
		}		
		return rm.toJson();
	}
	
	@ResponseBody
	@RequestMapping(value= currentTeamShareApi)
	@ApiOperation(notes= currentTeamShareApi,httpMethod="POST",value="当前人直属下级列表")
	public String currentTeamShare(
			@ApiParam("id") @RequestParam(value="id",required=true)String id,
			@ApiParam("昵称") @RequestParam(value="username",required=false)String username
			) {
		ResponseModel rm = new ResponseModel();
		List<Customer> customers=customerService.queryCurrentChildCustomer(id);
		
		if(customers.size()>=0){
			rm.setStatus(ResponseStatus.success);
			List<String> params = new ArrayList<String>();
			Field[] fields = Customer.class.getDeclaredFields();
			for(Field field : fields){
				params.add(field.getName());
			}
			rm.putList("customers", customers, params);
		}	
		
		rm.put("username", username);
		return rm.toJson();
	}

	@ResponseBody
	@RequestMapping(value= searchTeamApi)
	@ApiOperation(notes= searchTeamApi,httpMethod="POST",value="搜索")
	public String searchTeam(
			@ApiParam("searchContent") @RequestParam(value="searchContent",required=true)String searchContent
			) {
		ResponseModel rm = new ResponseModel();
		
		Customer customer=customerService.queryModel(getLoginId()); 
		
		List<TeamShareCustomer>teamShareCustomers = customerService.queryWechatSearch(searchContent, customer.getId());
				
		if(teamShareCustomers.size()>=0){
			rm.setStatus(ResponseStatus.success);
			List<String> params = new ArrayList<String>();
			Field[] fields = TeamShareCustomer.class.getDeclaredFields();
			for(Field field : fields){
				params.add(field.getName());
			}
			rm.putList("teamShareCustomers", teamShareCustomers, params);
		}	
		
		return rm.toJson();
	}
}
